package com.irdstudio.apicenter.console.controller.appframe;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppFrameModule;
import com.irdstudio.apicenter.console.dm.module.ModuleEntity;
import com.irdstudio.apicenter.console.dm.util.FootPage;
import com.irdstudio.apicenter.console.dm.util.FootPageDao;
import com.irdstudio.apicenter.console.service.appframe.AppFrameModuleService;
import com.irdstudio.apicenter.console.service.appframe.AppModuleParamService;
import com.irdstudio.apicenter.frame.core.jdbc.tablemodel.ITableModelDAO;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appFrameModule")
public class AppFrameModuleAction extends MacmsBaseAction {
	@Autowired
	private AppFrameModuleService appFrameModuleService;
	@Autowired
	private AppModuleParamService appModuleParamService;

	@RequestMapping(value = "appFrameModuleList")
	public ModelAndView queryWithKeys(HttpServletRequest request)
			throws Exception {
		/*
		 * String appid=request.getParameter("appid"); String
		 * conditionSql="  where app_id='"+appid+"'"; ModelAndView modelAndView
		 * = new ModelAndView(); List<AppFrameModule> appFrameModuleList =
		 * appFrameModuleService.queryAppFrameModuleList("", "", conditionSql);
		 * modelAndView.addObject("AppFrameModuleList", appFrameModuleList);
		 * modelAndView.addObject("appid", appid);
		 * modelAndView.setViewName("appframe/module"); return modelAndView;
		 */
		String appid = request.getParameter("appid");
		List<HashMap<String, String>> Modulelist = appFrameModuleService
				.queryByAppid1(appid);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("Modulelist", Modulelist);
		modelAndView.addObject("appid", appid);
		modelAndView.setViewName("appframe/module");
		return modelAndView;

	}

	@RequestMapping(value = "modulelist")
	public String queryModule(HttpServletRequest request) throws SQLException,
			ClassNotFoundException, UnsupportedEncodingException {
		String moduleid = "";
		String modulename = "";
		if (request.getParameter("moduleid") != null) {
			moduleid = request.getParameter("moduleid");// 模块编码
		}
		if (request.getParameter("modulename") != null) {
			modulename = request.getParameter("modulename");// 模块编码
		}
		
		String appid = request.getParameter("appid");
		int row = 0;
		int size = 0;
		row = (null == request.getParameter("pageNo") ? 0 : Integer
				.parseInt(request.getParameter("pageNo")));
		size = (null == request.getParameter("pageSize") ? 0 : Integer
				.parseInt(request.getParameter("pageSize")));

		FootPageDao fpd = new FootPageDao();
		FootPage fp = new FootPage();
		fp = fpd
				.querypage(
						"  s_module_base  where  module_id  not  in   (select module_id  from app_frame_module  where app_id='1')   and  module_id  like  '%"
								+ moduleid
								+ "%'  and  module_name  like  '%"
								+ modulename + "%' ", row, size);
		row = (fp.getPageNo() - 1) * fp.getPageSize();

		String sql = "select  * from   s_module_base  where  module_id  not  in   (select module_id  from app_frame_module  where app_id='"
				+ appid
				+ "')   and  module_id  like  '%"
				+ moduleid
				+ "%'  and  module_name  like  '%" + modulename + "%'";
		ITableModelDAO tableModelDao = (ITableModelDAO) BeanUtils
				.getBean("tableModelDAO");
		sql = tableModelDao.getNativePageSql(sql, row, fp.getPageSize());
		List<ModuleEntity> modulelist = appFrameModuleService
				.queryModuleList(sql);
		request.setAttribute("appid", appid);
		request.setAttribute("modulelist", modulelist);
		request.setAttribute("page", fp);
		return "appframe/moduleList";

	}

	@RequestMapping(value = "insertAppFrameModule")
	public ModelAndView insertAppFrameModule(HttpServletRequest request)
			throws Exception {
		int flag = -1;
		String str1 = "";
		String appid = request.getParameter("appid");
		String str = request.getParameter("str");// moduleid用“,”拼成的字符串
		AppFrameModule appFrameModule = new AppFrameModule();
		appFrameModule.setAppId(appid);
		String moduleidList[] = str.split(",");
		ModelAndView modelAndView = new ModelAndView();
		for (int i = 0; i < moduleidList.length; i++) {
			appFrameModule.setModuleId(moduleidList[i]);
			flag = appFrameModuleService.insertAppFrameModule(appFrameModule);
			str1 = "insert into app_module_param  " + " select '" + appid
					+ "'            as app_id,"
					+ "      module_id           as module_id,"
					+ "      param_code          as param_code,"
					+ "      param_name          as param_name,"
					+ "      '1'                 as is_must_need,"
					+ "      param_default_value as param_value"
					+ " from s_module_param" + " where module_id = '"
					+ moduleidList[i] + "'";
			appModuleParamService.addModuleParam(str1);
		}
		modelAndView.addObject("iresult", flag);
		// modelAndView.setViewName("appframe/module");
		modelAndView
				.setViewName("redirect:/appFrameModule1/appFrameModuleList1?appid="
						+ appid);
		return modelAndView;
	}

	@RequestMapping(value = "deleteAppFrameModule")
	public ModelAndView deleteAppFrameModule(
			@ModelAttribute("appid") String appid,
			@ModelAttribute("moduleid") String moduleid,
			HttpServletRequest request) throws Exception {
		String id = request.getParameter("appid");
		String mid = request.getParameter("moduleid");
		ModelAndView modelAndView = new ModelAndView();
		int flag = appFrameModuleService.deleteAppFrameModule(id, mid);
		List<AppFrameModule> appFrameModuleList = appFrameModuleService
				.queryAppFrameModuleList("", "", "");
		modelAndView.addObject("AppFrameModuleList", appFrameModuleList);
		modelAndView.addObject("result", flag);
		modelAndView.setViewName("appframe/module");
		return modelAndView;
	}

	/**
	 * 模块页面、模块参数
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("appid");
		String mid = request.getParameter("moduleid");
		modelAndView.addObject("appid", id);
		modelAndView.addObject("moduleid", mid);
		modelAndView.setViewName("appframe/moduleEdit");
		return modelAndView;
	}

	/**
	 * 模块详情
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "moduleDetail")
	public ModelAndView moduleDetail(HttpServletRequest request)
			throws Exception {
		String moduleid = request.getParameter("moduleid");
		String str[] = new String[3];
		str = appFrameModuleService.queryModuleDetail(moduleid);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("str", str);
		modelAndView.setViewName("appframe/moduleInfoEdit");
		return modelAndView;
	}

	/**
	 * 模块组成页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "ModulePages")
	public String getModulePages(HttpServletRequest request) throws Exception {
		return "appframe/pageFileList";
	}

}
